package nz;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

public class LibraryStatsApp extends JFrame {

    private final JButton pieChartButton;
    private final JButton barChartButton;
    private final CardLayout cardLayout;
    private final JPanel cardsPanel;

    public LibraryStatsApp() {
        // 设置主窗口
        setTitle("图书统计系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建按钮
        pieChartButton = new JButton("查看图书类别百分比");
        barChartButton = new JButton("查看图书类别借阅人数柱状图");

        // 添加按钮到界面顶部
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(pieChartButton);
        buttonPanel.add(barChartButton);
        add(buttonPanel, BorderLayout.NORTH);

        // 使用 CardLayout 管理不同的图表面板
        cardLayout = new CardLayout();
        cardsPanel = new JPanel(cardLayout);
        add(cardsPanel, BorderLayout.CENTER);

        // 获取数据
        Map<String, Integer> categoryPercentage = getBookCategoryPercentage();
        Map<String, Integer> categoryBorrowing = getBookCategoryBorrowing();

        // 添加图表面板到 CardLayout
        cardsPanel.add(new PieChartPanel(categoryPercentage, "图书类别百分比"), "PieChart");
        cardsPanel.add(new BarChartPanel(categoryBorrowing, "图书类别借阅人数柱状图"), "BarChart");

        // 设置按钮事件监听器
        pieChartButton.addActionListener(e -> cardLayout.show(cardsPanel, "PieChart"));
        barChartButton.addActionListener(e -> cardLayout.show(cardsPanel, "BarChart"));
    }

    // 查询图书类别百分比数据
    private Map<String, Integer> getBookCategoryPercentage() {
        Map<String, Integer> data = new HashMap<>();
        String query = "SELECT Bcategory, COUNT(*) AS count FROM books GROUP BY Bcategory";

        // 使用try-with-resources管理数据库资源
        try (Connection conn = ConnectDB.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(query)) {
            while (rs.next()) {
                // 从结果集中获取书籍分类和对应的数量
                data.put(rs.getString("Bcategory"), rs.getInt("count"));
            }
        } catch (SQLException e) {
            e.printStackTrace();  // 错误处理，打印异常
        }
        return data;
    }


    // 查询图书类别借阅人数数据
    private Map<String, Integer> getBookCategoryBorrowing() {
        Map<String, Integer> data = new HashMap<>();
        String query = "SELECT b.Bcategory, COUNT(l.Uno) AS count " +
                "FROM lend l " +
                "JOIN books b ON l.Bno = b.Bno " +
                "GROUP BY b.Bcategory";

        // 使用try-with-resources来管理数据库资源
        try (Connection conn = ConnectDB.getConnection();  // 使用ConnectDB的getConnection方法来获取连接
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(query)) {
            // 遍历结果集，将数据放入Map
            while (rs.next()) {
                data.put(rs.getString("Bcategory"), rs.getInt("count"));
            }
        } catch (SQLException e) {
            e.printStackTrace();  // 错误处理，打印异常
        }

        return data;
    }


}
